package com.migu.rbac.service;

import com.migu.rbac.model.SysBackApi;
import com.migu.rbac.vo.PageInfoRespone;
import com.migu.rbac.repository.SysBackApiRepository;
import com.migu.rbac.vo.page.ApiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Description TODO :
 * @Author: zj
 * @Date: 9:57 2019/4/4
 * @Version 1.0
 **/
@Service
@Transactional
public class SysBackApiService {

    @Autowired
    SysBackApiRepository apiRepository;

    public List<SysBackApi> findByPathStartsWithAndMethod(Map<String, String> map) {
        return apiRepository.findByPathStartsWithAndMethod(map);
    }

    public SysBackApi findByPathAndMethod(Map<String, String> map) {
        return apiRepository.findByPathAndMethod(map);
    }

    public List<SysBackApi> findAllApi() {
        return apiRepository.findAllApi();
    }

    public boolean save(SysBackApi api) {
        int i = apiRepository.selectApiByProjectAndPath(api);
        if (i > 0) {
            return false;
        }
        apiRepository.saveApi(api);
        return true;
    }

    //删除url
    public Boolean deleteUrl(int id) {
        if (apiRepository.deleteUrl(id) == 0) {
            return false;
        }
        return true;
    }

    //修改url
    public boolean updateUrlById(SysBackApi sysBackApi) {
        if (apiRepository.updateUrlById(sysBackApi) == 0) {
            return false;
        }
        return true;
    }

    //分页查询url
    public PageInfoRespone selectApiByExample(ApiPage page) {
        PageInfoRespone respone = new PageInfoRespone();
        respone.setPageNum(page.getPageNum());
        respone.setPageSize(page.getPageSize());
        respone.setCount(apiRepository.selectApiCount());
        page.setPageNum((page.getPageNum() - 1) * page.getPageSize());
        respone.setList(apiRepository.selectApiByExample(page));
        return respone;
    }

    //获取id
    public Integer selectIDByPathAndMethod(String path, String method) {
        return apiRepository.selectIDByPathAndMethod(path, method);
    }

    //删除backapi by menuid
    public Boolean deleteBackApiByMenuId(int menuid) {
        if (apiRepository.deleteBackApiByMenuId(menuid) == 0) {
            return false;
        }
        return true;
    }

}